package edu.msg.hrappy.backend.repository.beans;

import edu.msg.hrappy.backend.model.Group;
import edu.msg.hrappy.backend.model.GroupType;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.repository.GroupRepository;
import edu.msg.hrappy.backend.repository.exception.RepositoryException;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;


@Stateless (name = "GroupRepository")
public class GroupRepositoryBean extends BaseRepositoryBean<Group, Long> implements GroupRepository{

	public GroupRepositoryBean() {
		super(Group.class);
	}

    @Override
    public Group getByName(String name) {
        try {
            TypedQuery<Group> query = (TypedQuery<Group>) getManager ().createQuery (
                    "SELECT g FROM Group g WHERE g.typeName = :name");
            query.setParameter ("name", GroupType.valueOf(name));
            
            if (query.getResultList ().isEmpty ()) {
                return null;
            } else {
                return query.getSingleResult ();
            }
        } catch (final javax.ejb.TransactionRolledbackLocalException ex) {
            throw new RepositoryException ("Get by name failed", ex);
        }
    }

}
